package com.ad.pet.app.main.concer;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.main.adapter.PetConcerListAdapter;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.BaseActivity;
import com.ad.pet.models.remote.login.Country;
import com.ad.pet.models.remote.main.DesingerStore;
import com.ad.pet.models.remote.main.PetConcer;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liuyixiang on 2019/11/20.
 */

public class PetConcerListActivity extends BaseActivity  implements PetConcerContract.View{


    @Inject
    PetConcerPresenter petConcerPresenter;

    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefresh;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.recycelr_store)
    RecyclerView recycelrStore;

    DesingerStore desingerStore;
    String myLong;
    String mylat;
    int page=1;
    private PetConcerListAdapter petConcerListAdapter;

    @Override
    protected void initViews() {
        petConcerPresenter.onCreate(this);
        myLong = getIntent().getStringExtra("long");
        mylat = getIntent().getStringExtra("lan");
        desingerStore = (DesingerStore) getIntent().getSerializableExtra("Store");
        petConcerListAdapter = new PetConcerListAdapter(null);
        recycelrStore.setLayoutManager(new LinearLayoutManager(mActivity));
        recycelrStore.setAdapter(petConcerListAdapter);
        petConcerListAdapter.bindToRecyclerView(recycelrStore);


        tvCity.setText(desingerStore.getCity_name());
    }

    @Override
    protected void initData() {


        petConcerPresenter.getPetConcers(myLong,mylat,String.valueOf(page),"",desingerStore.getProvince_id());
    }

    @Override
    protected void initEvents() {
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (TextUtils.isEmpty(etSearch.getText().toString().trim())) {
                    ToastUtil.getToastUtil().showShort(getString(R.string.please_input_serach_content));
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    petConcerPresenter.getPetConcers(myLong,mylat,String.valueOf(page),etSearch.getText().toString(),desingerStore.getProvince_id());
                    return true;
                }
                return false;
            }
        });

        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(etSearch.getText().toString())) {
                    etSearch.setText("");
                    page=1;
                    petConcerPresenter.getPetConcers(myLong,mylat,String.valueOf(page),"",desingerStore.getProvince_id());
                }
            }
        });

        petConcerListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                petConcerPresenter.getPetConcers(myLong,mylat,String.valueOf(page),"",desingerStore.getProvince_id());
            }
        }, recycelrStore);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefresh.setRefreshing(true);
                page = 1;
                petConcerPresenter.getPetConcers(myLong,mylat,String.valueOf(page),"",desingerStore.getProvince_id());
            }
        });

        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goChoseCountry(mActivity,100);
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_pet_concer_list;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {
        ToastUtil.getToastUtil().showShort(msg);
    }

    @Override
    public void getPetConcersSuccess(List<PetConcer> cates) {
        mSwipeRefresh.setRefreshing(false);


        if (page == 1) {
            petConcerListAdapter.setNewData(cates);
        } else {
            petConcerListAdapter.addData(cates);
            petConcerListAdapter.loadMoreComplete();
        }

        if (cates.size() < 10) {//CommonConstant.PAGE_COUNT
            //暂无更多数据
            petConcerListAdapter.loadMoreEnd();
        }
        if (cates.size()==0&&page==1){
            petConcerListAdapter.setEmptyView(R.layout.layout_empty_address);
            ((TextView) petConcerListAdapter.getEmptyView().findViewById(R.id.tv_title)).setText("暂无宠物角~");
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data == null)
            return;
        switch (requestCode) {
            case 100:
                Country   country = (Country) data.getSerializableExtra("country");
                tvCity.setText(country.getCity_name());
                page=1;
                desingerStore.setProvince_id(country.getProvince_id());
                petConcerPresenter.getPetConcers(myLong,mylat,String.valueOf(page),"",desingerStore.getProvince_id());
                break;
        }


    }
}
